import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '../views/Manger/Login.vue';
import Home from '../views/Manger/Home.vue';
import Welcome from '../views/Manger/Welcome.vue';
import ModifyPassword from '../views/Manger/ModifyPassword.vue';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import OrdersManger from '../views/Manger/OrdersManger.vue';
import OrdersAddOrEdit from '../views/Manger/OrdersAddOrEdit.vue';
import AddresssManger from '../views/Manger/AddresssManger.vue';
import AddresssAddOrEdit from '../views/Manger/AddresssAddOrEdit.vue';
import CommentsManger from '../views/Manger/CommentsManger.vue';
import CommentsAddOrEdit from '../views/Manger/CommentsAddOrEdit.vue';
import UsersManger from '../views/Manger/UsersManger.vue';
import UsersAddOrEdit from '../views/Manger/UsersAddOrEdit.vue';
import ProductsManger from '../views/Manger/ProductsManger.vue';
import ProductsAddOrEdit from '../views/Manger/ProductsAddOrEdit.vue';
import AdminsManager from '../views/Manger/AdminsManager.vue';
import AdminsAddOrEdit from '../views/Manger/AdminsAddOrEdit.vue';
import CategoryManager from '../views/Manger/CategoryManager.vue';
import CategoryAddOrEdit from '../views/Manger/CategoryAddOrEdit.vue';
import AdminInfo from '../views/Manger/AdminInfo.vue';
Vue.use(VueRouter);
const routes = [
  {
    //面向管理员与商家的登录窗口
    path: '/login',
    component: Login,
  },
  // {
  //   path: '/about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () =>
  //     import(/* webpackChunkName: "about" */ '../views/About.vue'),
  // },
  {
    path: '/home',
    component: Home,
    // beforeEnter: (to, from, next) => {
    //   // ...
    // }
    children: [
      {
        path: '/welcome.html',
        Nmae: Welcome,
        component: Welcome,
      },
      {
        path: '/modifyPassword.html',
        component: ModifyPassword,
      },
      {
        path: '/orders.html',
        component: OrdersManger,
      },
      {
        path: '/orders/toAdd.html',
        component: OrdersAddOrEdit,
      },
      {
        path: '/orders/toEdit/:id.html',
        component: OrdersAddOrEdit,
      },
      {
        path: '/account/users.html',
        component: UsersManger,
      },
      {
        path: '/account/users/toAdd.html',
        component: UsersAddOrEdit,
      },
      {
        path: '/account/users/toEdit/:id.html',
        component: UsersAddOrEdit,
      },
      {
        path: '/goods/products.html',
        component: ProductsManger,
      },
      {
        path: '/goods/products/toEdit/:id.html',
        component: ProductsAddOrEdit,
      },
      {
        path: '/goods/products/toAdd.html',
        component: ProductsAddOrEdit,
      },
      {
        path: '/goods/comment.html',
        component: CommentsManger,
      },
      {
        path: '/goods/comment/toAdd.html',
        component: CommentsAddOrEdit,
      },
      {
        path: '/goods/comment/toEdit/:id.html',
        component: CommentsAddOrEdit,
      },
      {
        path: '/account/address.html',
        component: AddresssManger,
      },
      {
        path: '/account/address/toAdd.html',
        component: AddresssAddOrEdit,
      },
      {
        path: '/account/address/toEdit/:id.html',
        component: AddresssAddOrEdit,
      },
      {
        path: '/account/admins.html',
        component: AdminsManager,
      },
      {
        path: '/account/admins/toAdd.html',
        component: AdminsAddOrEdit,
      },
      {
        path: '/account/admins/toEdit/:id.html',
        component: AdminsAddOrEdit,
      },
      {
        path: '/goods/category.html',
        component: CategoryManager,
      },
      {
        path: '/goods/category/toAdd.html',
        component: CategoryAddOrEdit,
      },
      {
        path: '/goods/category/toEdit/:id.html',
        component: CategoryAddOrEdit,
      },

      {
        path: '/duty/role.html',
        component: UsersManger,
      },
      {
        path: '/duty/role/toAdd.html',
        component: UsersAddOrEdit,
      },
      {
        path: '/AdminInfo.html',
        component: AdminInfo,
      },
    ],
  },
];

const router = new VueRouter({
  mode:'history',
  routes,
});

router.beforeEach((to, from, next) => {
  NProgress.start();
  const tokenStr = sessionStorage.getItem('token');
  if (to.path.startsWith('/login')) return next();
  else if (to.path.startsWith('/Register')) return next();
  else if (!tokenStr) return next('/login');
  next();
});

router.afterEach(() => {
  NProgress.done();
});


export default router;
